<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%@ taglib uri="http://www.springframework.org/tags" prefix="spring"%>
<%@ taglib uri="http://www.springframework.org/tags/form" prefix="form"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>

<%
	String path = request.getContextPath();
%>

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>easyUI Demo</title>
<link rel="stylesheet" type="text/css" href="<%=path%>/css/main.css">
<link rel="stylesheet" type="text/css" href="<%=path %>/themes/default/easyui.css"/>
<link rel="stylesheet" type="text/css" href="<%=path %>/themes/icon.css"/>
<script type="text/javascript" src="<%=path %>/js/jquery.min.js"></script>
<script type="text/javascript" src="<%=path %>/js/jquery.easyui.min.js"></script>
<script type="text/javascript" src="<%=path%>/js/easyui-lang-zh_CN.js"></script>
<script type="text/javascript" src="<%=path%>/js/common.js"></script>
<script type="text/javascript">
var path = '<%=path%>';
function addTab(title, url){
	if ($('#tabs').tabs('exists', title)){
		$('#tabs').tabs('select', title);//选中并刷新
		var currTab = $('#tabs').tabs('getSelected');
		var url = $(currTab.panel('options').content).attr('src');
		if(url != undefined && currTab.panel('options').title != '首页') {
			$('#tabs').tabs('update',{
				tab:currTab,
				options:{
					content:createFrame(url)
				}
			})
		}
	} else {
		var content = createFrame(url);
		$('#tabs').tabs('add',{
			title:title,
			content:content,
			closable:true
		});
	}
	tabClose();
}

function createFrame(url) {
	var s = '<iframe scrolling="auto" frameborder="0" src="'+url+'" style="width:100%; height:100%;"></iframe>';
	return s;
}
		
function tabClose() {
	/*双击关闭TAB选项卡*/
	$(".tabs-inner").dblclick(function(){
		var subtitle = $(this).children(".tabs-closable").text();
		$('#tabs').tabs('close',subtitle);
	})
	/*为选项卡绑定右键*/
	$(".tabs-inner").bind('contextmenu',function(e){
		$('#mm').menu('show', {
			left: e.pageX,
			top: e.pageY
		});

		var subtitle =$(this).children(".tabs-closable").text();

		$('#mm').data("currtab",subtitle);
		$('#tabs').tabs('select',subtitle);
		return false;
	});
}

//绑定右键菜单事件
function tabCloseEven() {
	//刷新
	$('#mm-tabupdate').click(function(){
		var currTab = $('#tabs').tabs('getSelected');
		var url = $(currTab.panel('options').content).attr('src');
		if(url != undefined && currTab.panel('options').title != '首页') {
			$('#tabs').tabs('update',{
				tab:currTab,
				options:{
					content:createFrame(url)
				}
			})
		}
	})
	//关闭当前
	$('#mm-tabclose').click(function(){
		var currtab_title = $('#mm').data("currtab");
		$('#tabs').tabs('close',currtab_title);
	})
	//全部关闭
	$('#mm-tabcloseall').click(function(){
		$('.tabs-inner span').each(function(i,n){
			var t = $(n).text();
			if(t != '首页') {
				$('#tabs').tabs('close',t);
			}
		});
	});
	//关闭除当前之外的TAB
	$('#mm-tabcloseother').click(function(){
		var prevall = $('.tabs-selected').prevAll();
		var nextall = $('.tabs-selected').nextAll();		
		if(prevall.length>0){
			prevall.each(function(i,n){
				var t=$('a:eq(0) span',$(n)).text();
				if(t != '首页') {
					$('#tabs').tabs('close',t);
				}
			});
		}
		if(nextall.length>0) {
			nextall.each(function(i,n){
				var t=$('a:eq(0) span',$(n)).text();
				if(t != '首页') {
					$('#tabs').tabs('close',t);
				}
			});
		}
		return false;
	});
	//关闭当前右侧的TAB
	$('#mm-tabcloseright').click(function(){
		var nextall = $('.tabs-selected').nextAll();
		if(nextall.length==0){
			//msgShow('系统提示','后边没有啦~~','error');
			alert('后边没有啦~~');
			return false;
		}
		nextall.each(function(i,n){
			var t=$('a:eq(0) span',$(n)).text();
			$('#tabs').tabs('close',t);
		});
		return false;
	});
	//关闭当前左侧的TAB
	$('#mm-tabcloseleft').click(function(){
		var prevall = $('.tabs-selected').prevAll();
		if(prevall.length==0){
			alert('到头了，前边没有啦~~');
			return false;
		}
		prevall.each(function(i,n){
			var t=$('a:eq(0) span',$(n)).text();
			$('#tabs').tabs('close',t);
		});
		return false;
	});

	//退出
	$("#mm-exit").click(function(){
		$('#mm').menu('hide');
	})
}
<%-- 
$(function() {

	$('.cs-navi-tab').click(function() {
		var $this = $(this);
		var href = $this.attr('src');
		var title = $this.text();
		addTab(title, href);
	});

	var themes = {
		'gray' : '<%=path%>/themes/gray/easyui.css',
		'black' : '<%=path%>/themes/black/easyui.css',
		'bootstrap' : '<%=path%>/themes/bootstrap/easyui.css',
		'default' : '<%=path%>/themes/default/easyui.css',
		'metro' : '<%=path%>/themes/metro/easyui.css'
	};

	var skins = $('.li-skinitem span').click(function() {
		var $this = $(this);
		if($this.hasClass('cs-skin-on')) return;
		skins.removeClass('cs-skin-on');
		$this.addClass('cs-skin-on');
		var skin = $this.attr('rel');
		$('#swicth-style').attr('href', themes[skin]);
		setCookie('cs-skin', skin);
		skin == 'dark-hive' ? $('.cs-north-logo').css('color', '#FFFFFF') : $('.cs-north-logo').css('color', '#000000');
	});
	if(getCookie('cs-skin')) {
		var skin = getCookie('cs-skin');
		$('#swicth-style').attr('href', themes[skin]);
		$this = $('.li-skinitem span[rel='+skin+']');
		$this.addClass('cs-skin-on');
		skin == 'dark-hive' ? $('.cs-north-logo').css('color', '#FFFFFF') : $('.cs-north-logo').css('color', '#000000');
	}
});
 --%>
function setCookie(name,value) {//两个参数，一个是cookie的名子，一个是值
    var Days = 30; //此 cookie 将被保存 30 天
    var exp = new Date();    //new Date("December 31, 9998");
    exp.setTime(exp.getTime() + Days*24*60*60*1000);
    document.cookie = name + "="+ escape (value) + ";expires=" + exp.toGMTString();
}

function getCookie(name) {//取cookies函数        
    var arr = document.cookie.match(new RegExp("(^| )"+name+"=([^;]*)(;|$)"));
    if(arr != null) return unescape(arr[2]); return null;
}

function dateFormat(date){
	if(date < 10){
		return "0" + date;
	}else{
		return date;
	}
}

/* 修改密码 */
function updatePWD(){
	$("#pwd_form").form('submit', {
		url: '<%=path%>/common/updatePwd.do',
		success: function(data){
			var result = $.parseJSON(data);
			if(result == 'success'){
				$.messager.alert('温馨提示','密码修改成功！', 'info', function(){
					$("#pwd_form").form("clear");
					$('#updatePassword').window('close');
        		});
			}
			if(result == 'error'){
				$.messager.alert('温馨提示','原始密码错误！', 'error', function(){
					$("#pwd_form").form("clear");
        		});
			}
			if(result == 'fail'){
				$.messager.alert('温馨提示','系统出现错误，请联系管理员！', 'error', function(){
					$("#pwd_form").form("clear");
					$('#updatePassword').window('close');
        		});
			}
		}
	});
}

/* 加载备忘录 */
function loadMemorandum(){

	loadDictCombobox(['memorandumComplete'], 'memorStatus');

	var url = '<%=path%>/memorandum/loadMemorandum.do';
	$.post(url, {}, function(dataArray){
		window.sessionStorage.removeItem("memorandumList");
		window.sessionStorage.setItem("memorandumList", dataArray);
		/* 备忘录 */
		$('#cc').calendar({
		    current: new Date(),
		    formatter: function(date){
				var cDate = date.getFullYear() + "-" + dateFormat((date.getMonth()+1)) + "-" + dateFormat(date.getDate());
		    	var result = date.getDate();
				$.each(JSON.parse(dataArray), function(index, object){
		    		var sDate = object.memorandumDate;
		    		if(cDate == sDate){
		    			if(object.memorandumComplete == 'memorStatus1'){
		    				result = '<div class="icon-ok md">' + date.getDate() + '</div>';
		    			}else{
		    				result = '<div class="icon-search md">' + date.getDate() + '</div>';
		    			}
		    		}
		    	});
		    	return result;
			},
		    onSelect: function(date){
		    	var selectDate = date.getFullYear() + "-" + dateFormat((date.getMonth()+1)) + "-" + dateFormat(date.getDate());
		    	var memorandumArray = window.sessionStorage.getItem("memorandumList");
		    	var memorandum = {
	    			"memorandumId" : "",
	    			"memorandumDate" : selectDate,
	    			"memorandumComplete" : "memorStatus0",
	    			"memorandumTitle" : "",
	    			"memorandumContent" : ""
		    	};
		    	$.each(JSON.parse(memorandumArray), function(index, object){
		    		if(selectDate == object.memorandumDate){
		    			memorandum = object;
		    		}
		    	});
	    		$('#memorandumId').val(memorandum.memorandumId);
		    	$('#memorandumDate').datebox('setValue', memorandum.memorandumDate);
		    	$('#memorandumComplete').combobox('setValue', memorandum.memorandumComplete);
		    	$('#memorandumTitle').textbox('setValue', memorandum.memorandumTitle);
		    	$('#memorandumContent').textbox('setValue', memorandum.memorandumContent);
				openMemorandum();
			}
		});
	});
}

/* 打开备忘录 */
function openMemorandum(){
	$('#memorandum').dialog({
		title: '待办内容',
		closed: false,
		cache: false,
		modal: true,
		resizable: false,
		draggable: false,
		buttons: [{
			iconCls: 'icon-save',
			text: '保存',
			handler: function(){
				saveMemorandum();
			}
		}]			
	});
}

/* 保存备忘录 */
function saveMemorandum(){
	$("#memorandum_form").form('submit', {
		url: '<%=path%>/memorandum/saveMemorandum.do',
		success: function(data){
			if(JSON.parse(data).status == 0){
				$.messager.alert('温馨提示','备忘录保存成功！', 'info', function(){
					$("#memorandum_form").form("clear");
					$('#memorandum').window('close');
					loadMemorandum();
        		});
			}
			if(JSON.parse(data).status == 1){
				$.messager.alert('温馨提示','备忘录保存失败，请联系管理员！', 'error');
			}
		}
	});
}

$(document).ready(function(){
	
	var themes = {
		'gray' : '<%=path%>/themes/gray/easyui.css',
		'black' : '<%=path%>/themes/black/easyui.css',
		'bootstrap' : '<%=path%>/themes/bootstrap/easyui.css',
		'default' : '<%=path%>/themes/default/easyui.css',
		'metro' : '<%=path%>/themes/metro/easyui.css'
	};
	
	var skin = 'default';
	$('#swicth-style').attr('href', themes[skin]);
	skin == 'dark-hive' ? $('.cs-north-logo').css('color', '#FFFFFF') : $('.cs-north-logo').css('color', '#000000');
	
	/* 重写校验方法，添加新规则 */
	$.extend($.fn.validatebox.defaults.rules, {
	    equals: {
	        validator: function(value,param){
	            return value == $(param[0]).val();
	        },
	        message: '两次新密码不一致!'
	    }
	});
	
	/* 加载右键菜单 */
	tabCloseEven();
	
	/* 加载备忘录 */
	loadMemorandum();
	
	/* 加载系统菜单 */
	$("#systemMenu").tree({
		url: '<%=path%>/common/getSystemMenu.do',
		method: 'post',
		//queryParams: {"id" : 0},
		animate: true,
		checkbox: false,
		onClick: function(node){
			if(node.id != ''){
				addTab(node.text, '<%=path%>'+node.id);
			}
		}
	});
	
	/* 加载快速链接 */
	$("#webMenu").tree({
		url: '<%=path%>/common/getWebMenu.do',
		method: 'post',
		//queryParams: {"id" : 0},
		animate: true,
		checkbox: false,
		onClick: function(node){
			if(node.id != ''){
				addTab(node.text, node.id);
			}
		}
	});
	
	/* 修改密码 */
	$("#updatePass").click(function(){
		$('#updatePassword').dialog({
			title: '修改密码',
			width: 300,
			closed: false,
			cache: false,
			modal: true,
			resizable: false,
			draggable: false,
			buttons: [{
				iconCls: 'icon-save',
				text: '确定',
				handler: function(){
					updatePWD();
				}
			}]			
		});
	});
	
	/* 退出系统 */
	$("#exitSystem").click(function(){
		$.messager.confirm('温馨提示','确定退出系统？', function(y){
			if(y){
				var exitURL = "<%=path%>/common/exitSystem.do";
				var param = {};
				$.post(exitURL, param, function(data){
					window.location.href = "<%=path%>/pages/login.jsp";
				});
			}else {
				return false;
			}
		});
	});
	
	$.messager.show({
        title: 'welcome',
        msg: '欢迎'+$('#trueName').val()+'登陆系统！',
        timeout: 5000,
        showType: 'slide'
    });
	
	//$('body').layout('collapse', 'west');
});
</script>
</head>
<body class="easyui-layout">
	<div region="north" border="true" class="cs-north">
		<form:hidden path="userInfo.trueName" id="trueName"/>
		<div style="float: left;">
			<img alt="portrait" src="<%=path%>/images/portrait.jpg" style="width: 78px; height: 78px;"/>
		</div>
		<!-- 
		<ul class="ui-skin-nav">
			<li class="li-skinitem" title="default"><span class="default" rel="default"></span></li>
			<li class="li-skinitem" title="gray"><span class="gray" rel="gray"></span></li>
			<li class="li-skinitem" title="bootstrap"><span class="bootstrap" rel="bootstrap"></span></li>
			<li class="li-skinitem" title="black"><span class="black" rel="black"></span></li>
			<li class="li-skinitem" title="metro"><span class="metro" rel="metro"></span></li>
		</ul> -->
		<div style="float: right; margin: 50px 10px 0px 0px">
			<a id="updatePass" href="javascript:void(0);">修改密码</a><br>
			<a id="exitSystem" href="javascript:void(0);">退出系统</a>
		</div>
	</div>
	<div region="west" border="true" split="true" title="菜单栏" class="cs-west">
		<div class="easyui-accordion" fit="true" border="false">
			<div title="系统菜单">
				<ul id="systemMenu"></ul>
			</div>
			<div title="快速链接">
				<ul id="webMenu"></ul>
			</div>
		</div>
	</div>
	<div id="mainPanle" region="center" border="true" border="false">
		<div id="tabs" class="easyui-tabs" fit="true" border="false">
		 	<div title="首页">
				<div id="cc" style="float:right; margin:5px; width:250px; height:250px;"></div>
				<div class="cs-home-remark"></div>
			</div>
        </div>
	</div>
	<div region="south" border="false" id="south"><center>jQuery EasyUI 1.4 Demo</center></div>
	<div id="updatePassword" class="easyui-dialog" data-options="closed: true">
		<form id="pwd_form" method="post">
			<table width="100%;" align="center" border="0" style="padding: 5px;">
				<tr>
					<td align="right">原始密码：</td>
					<td><input type="password" id="oldpwd" name="oldpwd" class="easyui-textbox" data-options="required:true" missingMessage="原始密码必须填写"></td>
				</tr>
				<tr>
					<td align="right">新密码：</td>
					<td><input type="password" id="newpwd" name="newpwd" class="easyui-textbox" data-options="required:true" missingMessage="新密码必须填写"></td>
				</tr>
				<tr>
					<td align="right">确认新密码：</td>
					<td><input type="password" id="newpwd2" name="newpwd2" class="easyui-textbox" data-options="required:true" missingMessage="确认新密码必须填写" validType="equals['#newpwd']"></td>
				</tr>
			</table>
		</form>
	</div>
	<div id="memorandum" class="easyui-dialog" data-options="closed: true">
		<form id="memorandum_form" method="post">
			<input type="hidden" id="memorandumId" name="memorandumId">
			<table style="margin: 10px;">
				<tr>
					<th align="right"><h4>日期：</h4></th>
					<th align="left">
						<input type="text" id="memorandumDate" name="memorandumDate" class="easyui-datebox" 
							data-options="required:true" missingMessage="日期必须填写" style="width: 100px">
					</th>
					<th align="right"><h4>状态：</h4></th>
					<th align="left">
						<input type="text" id="memorandumComplete" name="memorandumComplete" class="easyui-combobox"
							data-options="required:true" missingMessage="状态必须填写" style="width: 100px">
					</th>
				</tr> 
				<tr>
					<th align="right"><h4>标题：</h4></th>
					<th align="left" colspan="3">
						<input type="text" id="memorandumTitle" name="memorandumTitle" class="easyui-textbox" data-options="required:true" 
							missingMessage="标题必须填写" style="width: 300px;">
					</th>
				</tr>
				<tr>
					<th align="right"><h4>内容：</h4></th>
					<th align="left" colspan="3">
						<input type="text" id="memorandumContent" name="memorandumContent" class="easyui-textbox" data-options="multiline:true" style="width: 300px; height: 100px;">
					</th>
				</tr>
			</table>
		</form>
	</div>
	<div id="mm" class="easyui-menu cs-tab-menu">
		<div id="mm-tabupdate">刷新</div>
		<div class="menu-sep"></div>
		<div id="mm-tabclose">关闭</div>
		<div id="mm-tabcloseother">关闭其他</div>
		<div id="mm-tabcloseall">关闭全部</div>
	</div>
</body>
</html>
